package com.online.demo.utils;

import org.csource.fastdfs.ProtoCommon;


// fastDFS 防盗链工具
public class AtcSecurityUtils {

    private String fastDFSAuth = "sda1_hacker_";

    // 将url 转换成 防盗链url -- token 不正确，先搁置之后再处理
    public String getATCUrl(String url) {

        // http://192.168.157.129/group1/M00/00/00/wKidgV_YGKeAAUeCAAAACBj3piE484_big.txt
        // http://192.168.157.129/group1/M00/00/00/wKidgV_XiSqAAEONAAAAB5qB7vI83_big.html
        // http://192.168.157.129/group1/M00/00/00/wKidgV_Xf86AMYSLAAAAB5qB7vI08_big.html
        String substring = url.substring(url.indexOf("/")+1); // M00/00/00/wKidgV_Xf86AMYSLAAAAB5qB7vI08_big.html
        //unix时间戳 以秒为单位
        int ts = (int) (System.currentTimeMillis() / 1000);
        String secret_key = fastDFSAuth;
        String token=new String();
        try {
            token= ProtoCommon.getToken(substring, ts, secret_key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        StringBuilder sb = new StringBuilder("http://192.168.157.129/group1");
        sb.append(url);
        sb.append("?token=").append(token);
        sb.append("&ts=").append(ts);
        return sb.toString();

    }

    public static String getATCUrlStatic(String url){
        AtcSecurityUtils utils = new AtcSecurityUtils();
        return utils.getATCUrl(url);
    }

    public static void main(String[] args) {
        String res = AtcSecurityUtils.getATCUrlStatic("M00/00/00/wKidgV_XiSqAAEONAAAAB5qB7vI83_big.html");
        System.out.println(res);
    }
}
